package com.liao.badminton.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.liao.badminton.code.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.EntityUtils;
import com.liao.badminton.code.util.CheckUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.badminton.entity.SystemRoleMenu;
import com.liao.badminton.dto.SystemRoleMenuDto;
import com.liao.badminton.vo.SystemRoleMenuVo;
import com.liao.badminton.mapper.SystemRoleMenuMapper;
import com.liao.badminton.service.SystemRoleMenuService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
/**
 * @author liaocheng
 * @date 2024-10-30
 * description:角色和菜单关联表
 */
@Component
@Log4j2
public class SystemRoleMenuServiceImpl  extends ServiceImpl<SystemRoleMenuMapper, SystemRoleMenu> implements SystemRoleMenuService {


    /**
     * 新增
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Long> saveSystemRoleMenu(SystemRoleMenuDto param) throws ProjectException{
        SystemRoleMenu systemRoleMenu = param.invertEntity();
        EntityUtils.setCreatAndUpdateInfo(systemRoleMenu);
        this.save(param.invertEntity());
        return ResponseRest.suc("saveSystemRoleMenu", systemRoleMenu.getId());
    }
    /**
     * 删除
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> deletesSystemRoleMenu(SystemRoleMenuDto param) throws ProjectException {
        boolean bool = this.removeByIds(param.getDelIds());
        if (bool) {
            param.getDelIds().forEach(id -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "systemRoleMenu", "id", id));
        }
        return ResponseRest.suc("deletesSystemRoleMenu");
    }
    /**
     * 修改
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> updateSystemRoleMenu(SystemRoleMenuDto param)  throws ProjectException{
        SystemRoleMenu systemRoleMenu = param.invertEntity();
        EntityUtils.setUpdatedInfo(systemRoleMenu);
        boolean bool = this.updateById(systemRoleMenu);
        if (bool) {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "systemRoleMenu", "id", param.getId());
        }
        return ResponseRest.suc("updateSystemRoleMenu");
    }
    /**
     * 查询列表
     */
    @Override
    public ResponseRest findSystemRoleMenuByPageParam(PageParam<SystemRoleMenuDto> pageParam)  throws ProjectException{
        CheckUtil.checkValue(pageParam,"pageParam");
        ResponseRest resp = ResponseRest.suc("findSystemRoleMenuByPageParam");
        PageHelper.startPage(pageParam.getCurrent(), pageParam.getSize());
        LambdaQueryWrapper<SystemRoleMenu> query = new LambdaQueryWrapper<>();
        List<SystemRoleMenu> list = this.list(query);
        List<SystemRoleMenuVo> collect = list.stream().parallel().map(entity -> SystemRoleMenuVo.invertToVo(entity)).collect(Collectors.toList());
        resp.setData(new PageInfo<>(collect));
        return resp;
    }
    /**
     * 查询详情
     */
    @Override
    public ResponseRest findDetail(SystemRoleMenuDto param) throws ProjectException {
        Object getDate = RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "systemRoleMenu", "id", param.getId());
        if (isNotEmpty(getDate)) {
            RedisUtil.toFlushExpire(RedisEnum.CACHE_TABLE_DATA, "systemRoleMenu", "id", param.getId());
            return ResponseRest.suc("findDetail", (SystemRoleMenuVo) getDate);
        }
        SystemRoleMenuVo vo = null;
        SystemRoleMenu db = this.getById(param.getId());
        if (isNotEmpty(db)) {
            vo = SystemRoleMenuVo.invertToVo(db);
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA,"systemRoleMenu", "id", param.getId(),vo, null);
        }
        return ResponseRest.suc("findDetail", vo);
    }


    @Override
    public Set<Long> findRoleIdsByMenuId(Long menuId) {
        return this.getBaseMapper().findRoleIdsByMenuId(menuId);
    }
}
